package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IUserRegisterService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class RegisterController {

    @Reference(timeout = 5000,check = false)
    IKaptchaService iKaptchaService;

    @Reference(timeout = 5000,check = false, retries = 0)
    IUserRegisterService registerService;


    @PostMapping("/register")
    @Anoymous
    public ResponseData register(@RequestBody Map<String, String> map, HttpServletRequest request) {
        log.info("接收到了register请求" + map);
        // 验证校验码
        KaptchaCodeResponse kaptchaCodeResponse = verifyCaptchaCode(map, request);
        if (!SysRetCodeConstants.SUCCESS.getCode().equals(kaptchaCodeResponse.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        // 验证码没问题，接着开始注册用户信息
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setUserName(map.get("userName"));
        userRegisterRequest.setUserPwd(map.get("userPwd"));
        userRegisterRequest.setEmail(map.get("email"));

        UserRegisterResponse registerResponse
                = registerService.register(userRegisterRequest);
        if (!SysRetCodeConstants.SUCCESS.getCode().equals(registerResponse.getCode())) {
            return new ResponseUtil<>().setErrorMsg(registerResponse.getMsg());
        }

        // 正常返回，用户注册，只需要显示用户是否注册成功，客户端根据code判断即可
        return new ResponseUtil<>().setData(null);
    }

    /*
          验证用户输入的验证码
     */
    private KaptchaCodeResponse verifyCaptchaCode(Map<String, String> map, HttpServletRequest request) {
        // 获取用户输入的校验码
        String captcha = map.get("captcha");
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();

        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);

        return iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
    }

    @GetMapping("/verify")
    @Anoymous
    public ResponseData verifyMailActive(@RequestParam("uuid") String uuid,
                                         @RequestParam("username") String username) {

        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUserName(username);
        userVerifyRequest.setUuid(uuid);


        UserVerifyResponse verify = registerService.verify(userVerifyRequest);
        if (!SysRetCodeConstants.SUCCESS.getCode().equals(verify.getCode())) {
            return new ResponseUtil<>().setErrorMsg(verify.getMsg());
        }

        return new ResponseUtil<>().setData(null);

    }
}
